package com.twentyfour.controller;

import com.twentyfour.pojo.*;
import com.twentyfour.service.AdminServiceImpl;
import com.twentyfour.service.LeaderServiceImpl;
import com.twentyfour.service.RefereeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class IndexController {

    @Autowired
    @Qualifier("AdminServiceImpl")
    AdminServiceImpl adminService;

    @Autowired
    @Qualifier("LeaderServiceImpl")
    LeaderServiceImpl leaderService;

    @Autowired
    @Qualifier("RefereeServiceImpl")
    RefereeServiceImpl refereeService;


    @RequestMapping("/toMainAdmin")
    public String toMainAdmin(HttpSession session, Model model) {
        Admin admin = (Admin) session.getAttribute("admin");
        model.addAttribute("profile", session.getAttribute("profile"));
        model.addAttribute("admin", admin);


        return "admin/MainAdmin";
    }


    @RequestMapping("/toMainLeader")
    public String toMainLeader(HttpSession session, Model model) {
        Integer certainTeamId = (Integer) session.getAttribute("certainTeamId");
        List<Team> teams = adminService.queryAllTeam();
        for(Team team:teams){
            if (team.getAccount().equals(certainTeamId)){
                session.setAttribute("certainTeam",team);
                model.addAttribute("certainTeam",team);
            }
        }
        model.addAttribute("certainTeamId", certainTeamId);


        return "teamLeader/MainLeader";
    }


    @RequestMapping("/toMainReferee")
    public String toMainReferee(HttpSession session, Model model) {
        Integer certainRefereeId = (Integer) session.getAttribute("certainRefereeId");
        model.addAttribute("referee",session.getAttribute("referee"));
        model.addAttribute("certainRefereeId", certainRefereeId);
        model.addAttribute("profile",session.getAttribute("profile"));


        return "referee/MainReferee";
    }


    @RequestMapping("/toAthleteList")
    public String toAthleteList(HttpServletResponse response, HttpServletRequest request, HttpSession session,
                                Model model, @RequestParam("certainTeamId") Integer certainTeamId) {

        model.addAttribute("certainTeamId", certainTeamId);
        model.addAttribute("profile", session.getAttribute("profile"));
        List<Athlete> athletes = leaderService.queryAthletesByTeamAccount(certainTeamId);
        model.addAttribute("athletes", athletes);

        List<Athlete_Item> list = new ArrayList<>();

        for (Athlete athlete : athletes) {
            Integer athleteId = athlete.getAthleteId();
            List<Athlete_Item> athlete_items = leaderService.queryAthleteItemByAthleteId(athleteId);
            list.addAll(athlete_items);
        }

        model.addAttribute("list", list);

        return "teamLeader/AthleteList";
    }

    @RequestMapping("/toDoctorList")
    public String toDoctorList(HttpServletResponse response, HttpServletRequest request, HttpSession session,
                               Model model) {
        Integer certainTeamId = (Integer) session.getAttribute("certainTeamId");
        model.addAttribute("profile", session.getAttribute("profile"));
        model.addAttribute("certainTeamId", certainTeamId);
        model.addAttribute(certainTeamId);
        List<Doctor> doctors = leaderService.queryDoctorByTeamAccount(certainTeamId);
        model.addAttribute("doctors", doctors);


        return "teamLeader/DoctorList";
    }

    @RequestMapping("/toCoachList")
    public String toCoachList(HttpServletResponse response, HttpServletRequest request, HttpSession session,
                              Model model) {
        Integer certainTeamId = (Integer) session.getAttribute("certainTeamId");
        model.addAttribute("certainTeamId", certainTeamId);
        model.addAttribute("profile", session.getAttribute("profile"));
        List<Coach> coaches = leaderService.queryCoachByTeamAccount(certainTeamId);
        model.addAttribute("coaches", coaches);


        return "teamLeader/CoachList";
    }

    @RequestMapping("/toRefereeList")
    public String toRefereeList(HttpServletResponse response, HttpServletRequest request, HttpSession session,
                                Model model) {
        Integer certainTeamId = (Integer) session.getAttribute("certainTeamId");
        model.addAttribute("certainTeamId", certainTeamId);
        model.addAttribute("profile", session.getAttribute("profile"));
        model.addAttribute(certainTeamId);
        List<Referee> referees = leaderService.queryRefereeByTeamAccount(certainTeamId);
        model.addAttribute("referees", referees);


        return "teamLeader/RefereeList";
    }


    @RequestMapping("/toTeamList")
    public String toTeamList(Model model, HttpSession session) {

        Admin admin = (Admin) session.getAttribute("admin");
        model.addAttribute("admin", admin);
        model.addAttribute("profile", session.getAttribute("profile"));
        List<Team> teamList = adminService.queryAllTeam();
        model.addAttribute("teamList", teamList);

        return "admin/teamList";
    }

    @RequestMapping("/toItemList")
    public String toItemList(Model model, HttpSession session) {
        Admin admin = (Admin) session.getAttribute("admin");
        model.addAttribute("admin", admin);
        model.addAttribute("profile", session.getAttribute("profile"));
        List<Item> items = adminService.queryAllItems();
        model.addAttribute("items", items);

        return "admin/itemList";
    }

    @RequestMapping("/toAllRefereeList")
    public String toAllRefereeList(HttpServletResponse response, HttpServletRequest request, HttpSession session,
                                   Model model) {

        Admin admin = (Admin) session.getAttribute("admin");
        model.addAttribute("admin", admin);
        model.addAttribute("profile", session.getAttribute("profile"));
        List<Referee> referees = adminService.queryAllReferee();
        model.addAttribute("referees", referees);


        return "admin/refereeList";
    }


    @RequestMapping("/editGame")
    public String editGame(HttpSession session,
                           Model model,@RequestParam("itemId") Integer itemId){
        Admin admin = (Admin) session.getAttribute("admin");
        model.addAttribute("admin", admin);
        model.addAttribute("profile", session.getAttribute("profile"));
        List<Referee_Item> referee_items = adminService.queryAllRefereeByItemId(itemId);
        model.addAttribute("certainItemId",itemId);
        model.addAttribute("referee_items",referee_items);

        List<GameInformation> gameInformations = adminService.queryAllGameInformationByItemId(itemId);
        model.addAttribute("gameInformations",gameInformations);


        return "admin/gameDetail";
    }

    @RequestMapping("toGradeList")
    public String toGradeList(HttpSession session, Model model){
        Admin admin = (Admin) session.getAttribute("admin");
        model.addAttribute("admin", admin);
        model.addAttribute("profile", session.getAttribute("profile"));
        List<PersonalItemFinalScore> personalItemFinalScores = adminService.queryAllPersonalItemScore();
        model.addAttribute("personalItemFinalScores",personalItemFinalScores);

        List<TeamScore> teamScores = adminService.queryAllTeamScore();
        model.addAttribute("teamScores",teamScores);


        return "admin/GradeList";
    }

    @RequestMapping("/toGrade")
    public String grade(HttpSession session, Model model){
        Integer certainRefereeId = (Integer) session.getAttribute("certainRefereeId");
        model.addAttribute("profile",session.getAttribute("profile"));
        model.addAttribute("referee",session.getAttribute("referee"));
        model.addAttribute("certainRefereeId",certainRefereeId);
        List<RefereeScore> refereeScores = refereeService.queryAllItemsByRefereeId(certainRefereeId);
        model.addAttribute("refereeScores",refereeScores);

        return "referee/grade";
    }

    /*@RequestMapping("/toGameList")
    public String gameList(HttpServletResponse response, HttpServletRequest request, HttpSession session,
                           Model model,@RequestParam("itemId") Integer itemId){

        Integer certainRefereeId = (Integer) session.getAttribute("certainRefereeId");
        //List<GameInformation> gameInformations = refereeService.queryAllGameByRefereeId(5);
        //model.addAttribute("gameInformations",gameInformations);

        return "referee/gameList";
    }*/

    @RequestMapping("/toGradeConfirm")
    public String gradeConfirm(HttpServletResponse response, HttpServletRequest request, HttpSession session,
                               Model model){

        Integer certainRefereeId = (Integer) session.getAttribute("certainRefereeId");
        model.addAttribute("profile",session.getAttribute("profile"));
        model.addAttribute("referee",session.getAttribute("referee"));
        model.addAttribute("certainRefereeId",certainRefereeId);

        List<RefereeScore> refereeScores = refereeService.queryUncheckRefereeScoreByRefereeId(certainRefereeId);
        model.addAttribute("refereeScores",refereeScores);


        return "referee/gradeConfirm";
    }


    @RequestMapping("/scoreConfirmed")
    public void scoreConfirmed(HttpServletResponse response, HttpServletRequest request, HttpSession session,
                               Model model){

        PersonalItemFinalScore personalItemFinalScore = new PersonalItemFinalScore();



    }

}

